package com.hongkzh.www.look.enterprise.watch.presenter;

import com.hongkzh.www.look.enterprise.watch.model.bean.AdvByIdBean;
import com.hongkzh.www.look.enterprise.watch.model.bean.AdvRecommendByAdvIdBean;
import com.hongkzh.www.look.enterprise.userinfo.model.bean.MediaPhotoBean;
import com.hongkzh.www.look.enterprise.userinfo.model.bean.MediaProductBean;
import com.hongkzh.www.other.okhttp.CallBackUtil;
import com.hongkzh.www.other.okhttp.OkhttpUtil;
import com.hongkzh.www.look.enterprise.watch.view.iview.WatchIView;
import com.hongkzh.www.presenter.BasePresenter;

import okhttp3.Call;

import static com.hongkzh.www.other.api.Api.GETADVBYID;
import static com.hongkzh.www.other.api.Api.GETADVRECOMMENDBYADVID;
import static com.hongkzh.www.other.api.Api.GETALLALBUMBYUSERID;
import static com.hongkzh.www.other.api.Api.GETALLProductBYUSERID;

/**
 * 看视频
 */
public class WatchPresenter extends BasePresenter<WatchIView> {
    private int pageNumber = 1;
    private boolean lastPage = false;

    public WatchPresenter getAdvById(String id) {
        OkhttpUtil.okHttpPost(GETADVBYID(id), new CallBackUtil<AdvByIdBean>() {
            @Override
            public void onFailure(Call call, Exception e) {
                getIView().onError(e);
            }

            @Override
            public void onResponse(AdvByIdBean response) {
                getIView().setAdvById(response);
            }
        });
        return this;
    }

    public WatchPresenter getAdvRecommendByAdvId(String id) {
        OkhttpUtil.okHttpPost(GETADVRECOMMENDBYADVID(id), new CallBackUtil<AdvRecommendByAdvIdBean>() {
            @Override
            public void onFailure(Call call, Exception e) {
                getIView().onError(e);
            }

            @Override
            public void onResponse(AdvRecommendByAdvIdBean response) {
                getIView().setAdvRecommendByAdvId(response);
            }
        });
        return this;
    }

    public WatchPresenter getAllAlbumByUserId(String uid, int i) {
        OkhttpUtil.okHttpPost(GETALLALBUMBYUSERID(uid, String.valueOf(i), null, null), new CallBackUtil<MediaPhotoBean>() {
            @Override
            public void onFailure(Call call, Exception e) {
                getIView().onError(e);
            }

            @Override
            public void onResponse(MediaPhotoBean response) {
                getIView().setAllAlbumByUserId(response);
            }
        });
        return this;
    }

    public WatchPresenter getAllProductByUserId(String uid) {
        OkhttpUtil.okHttpPost(GETALLProductBYUSERID(uid, String.valueOf(pageNumber), null, null), new CallBackUtil<MediaProductBean>() {
            @Override
            public void onFailure(Call call, Exception e) {
                getIView().onError(e);
            }

            @Override
            public void onResponse(MediaProductBean response) {
                lastPage = response.getData().isLastPage();
                if (lastPage) getIView().onLastPage(lastPage);
                getIView().setAllProductByUserId(response);
            }
        });
        return this;
    }

    public void loadmore(String uid) {
        if (!lastPage) {
            pageNumber += 1;
            getAllProductByUserId(uid);
        }
    }
}
